﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;

/// <Questions>
/// How do you avoid declarations such as "Connections Connections;"
/// 


//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("YourAssembly.XmlSerializers")]

namespace Wumpus
{
    [DataContract]
    public class GameState
    {
        public GameState(List<List<int>> connections) { this.Connections = new Connections(connections); }
        public GameState(Position pos, List<List<int>> con) { Position = pos; Connections = new Connections(con); }
        public GameState(string filename) //load a game
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists("SavedGames")) store.CreateDirectory("SavedGames");

                using (IsolatedStorageFileStream stream = store.OpenFile("SavedGames\\" + filename + ".xml", System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(GameStateSave), new List<Type> { typeof (Player), typeof(Hazard), typeof(Inventory), typeof(List<List<int>>), typeof(List<Planet>), typeof(Planet) });
                    GameStateSave Save = (GameStateSave)serializer.ReadObject(stream);

                    Position position = new Position(Save.planets, Save.connections, Save.playerPosition);

                    this.Position = position;
                    this.Player = Save.player;
                    this.Connections = new Connections(Save.connections);
                }
            }
        }

        [DataMember] public Connections Connections; //TODO: make set() internal
        [DataMember] public Position Position;
        [DataMember] public Player Player;

        public void Save(string filename)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists("SavedGames")) store.CreateDirectory("SavedGames");

                //using (IsolatedStorageFileStream stream = store.OpenFile(filename + ".xml", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                using (IsolatedStorageFileStream stream = store.CreateFile("SavedGames\\" + filename + ".xml"))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(GameStateSave), new List<Type> { typeof(Player), typeof(Hazard), typeof(Inventory), typeof(List<List<int>>), typeof(List<Planet>), typeof(Planet) });

                    GameStateSave Save = GenerateSave();
                    serializer.WriteObject(stream, Save);
                }
            }
        }

        private GameStateSave GenerateSave()
        {
            GameStateSave Save = new GameStateSave();

            Save.connections = this.Connections.cons;
            Save.player = this.Player;
            Save.planets = this.Position.Planets;
            Save.playerPosition = this.Position.Player;

            return Save;
        }

    }

    [DataContract]
    public class GameStateSave //only to be used here
    {
        [DataMember] public List<List<int>> connections;
        [DataMember] public Player player;
        [DataMember] public List<Planet> planets;
        [DataMember] public int playerPosition;
    }

}